package com.jnzc.ocr.serv.shop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.jnzc.ocr.common.bean.Tree;
import com.jnzc.ocr.common.utils.TreeUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.dependencies.serv.mybatis.MybatisServiceImpl;
import com.jnzc.ocr.serv.shop.entity.ShopGoodsSku;
import com.jnzc.ocr.serv.shop.entity.ShopProductAttr;
import com.jnzc.ocr.serv.shop.entity.ShopProductSkuAttr;
import com.jnzc.ocr.serv.shop.filter.ShopGoodsSkuFilter;
import com.jnzc.ocr.serv.shop.helper.ShopGoodsSkuHelper;
import com.jnzc.ocr.serv.shop.mapper.ShopGoodsSkuMapper;
import com.jnzc.ocr.serv.shop.service.ShopGoodsSkuService;
import com.jnzc.ocr.serv.shop.service.ShopProductSkuAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zhangcm
 * @version 1.0, 2017-10-06
 */
@Service
public class ShopGoodsSkuServiceImpl extends MybatisServiceImpl<ShopGoodsSkuMapper, ShopGoodsSku, ShopGoodsSkuFilter> implements ShopGoodsSkuService {

    @Autowired
    private ShopProductSkuAttrService shopProductSkuAttrService;

    @Override
    protected void fillEntityWrapper(EntityWrapper<ShopGoodsSku> ew, ShopGoodsSkuFilter filter) {
        ShopGoodsSku entity = new ShopGoodsSku();
        entity.setId(filter.getId());
        entity.setProductId(filter.getProductId());
        entity.setShopId(filter.getShopId());
        ew.setEntity(entity);
    }

    @Override
    public ShopGoodsSku find(Long id, Long productId, Long shopId) {
        ValidateUtils.isId(id);
        ShopGoodsSkuFilter filter = new ShopGoodsSkuFilter();
        filter.setId(id);
        filter.setProductId(productId);
        filter.setShopId(shopId);
        return ShopGoodsSkuHelper.init(super.findNotNull(filter));
    }

    @Override
    public List<ShopGoodsSku> list(Long productId, Long shopId) {
        ValidateUtils.isId(productId);
        ShopGoodsSkuFilter filter = new ShopGoodsSkuFilter();
        filter.setProductId(productId);
        filter.setShopId(shopId);
        return ShopGoodsSkuHelper.init(super.list(filter));
    }

    @Override
    public List<ShopGoodsSku> listWithAttr(Long productId, Long shopId, List<Tree<ShopProductAttr>> attrTree) {
        Map<Long, List<ShopProductSkuAttr>> skuAttrMap = shopProductSkuAttrService.listSkuMapByProductId(productId, shopId);
        List<ShopGoodsSku> list = this.list(productId, shopId);
        for (ShopGoodsSku obj : list) {
            List<ShopProductSkuAttr> skuAttrList = skuAttrMap.get(obj.getId());
            List<ShopProductAttr> attrList = Lists.newArrayList();
            for (ShopProductSkuAttr skuAttr : skuAttrList) {
                ShopProductAttr attr = TreeUtils.find(attrTree, skuAttr.getAttrId());
                attrList.add(attr);
            }
            obj.setAttrList(attrList);
        }
        return list;
    }
}
